<?php 
	class Shelvess extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}

    	/**
        * 函数功能介绍：返回课程列表（用于分页显示）
        * 创建人:  Yang Ningshu
        * @param id int
        * @return array
        */
        function get_list_ofpage($limit = 0, $offset = 10,$data = 0){
            if($data['sj'] > 2){
                $where = "1=1 ";
            }else{
                $where = "1=1 and v.school_id = 0";
            }
            $order = "v.id desc";

            if($data['status'] != 10){
                $where .= " AND v.status = '". $data['status']."'";
            }

             if($data['type'] > 0){
                $where .= " AND v.type = '". $data['type']."'";
            }
            
            if($data['cate_id'] && $data['cate_id'] > 0){
                $where .= " AND v.cate_id = '". $data['cate_id']."'";
            }
            
            if(!empty($data['search'])) {
                $where .= " AND v.title like '%".$data['search']."%'";
            }

            if($data['sj'] == 1){
                $sql = "SELECT SQL_CALC_FOUND_ROWS v.*,cs.id as sjid,cs.fx_price,cs.commission,cs.expire_day,cs.add_time AS sj_time FROM {$this->db->dbprefix}course as v left join {$this->db->dbprefix}course_shelves as cs on(v.id = cs.course_id and cs.school_id =".$this->user_data['school_id'].") WHERE $where ORDER BY ".$order." LIMIT $limit, $offset";
            }elseif($data['sj'] == 2){
               $sj_sql = "SELECT SQL_CALC_FOUND_ROWS v.*,cs.id as sjid,cs.fx_price,cs.commission,cs.expire_day,cs.add_time AS sj_time FROM {$this->db->dbprefix}course_shelves as cs left join {$this->db->dbprefix}course as v on(v.id = cs.course_id and cs.is_fenxiao = 1) WHERE $where and cs.school_id =".$this->user_data['school_id']."";
                $sj_result = $this->db->query($sj_sql)->result_array();
                $sj_id = "";
                if(!empty($sj_result)){
                    foreach ($sj_result as $key => $value) {
                        $sj_id[] = $value['id'];
                    }
                    $sj_id = implode(',',$sj_id);   
                    $sj_id = " and v.id not in(".$sj_id.")"; 
                }
                $sql = "SELECT v.*,cs.id as sjid FROM {$this->db->dbprefix}course as v left join {$this->db->dbprefix}course_shelves as cs on(v.id = cs.course_id) WHERE $where $sj_id ORDER BY ".$order." LIMIT $limit, $offset";
            }elseif($data['sj'] == 3){
                $sql = "SELECT SQL_CALC_FOUND_ROWS v.*,cs.id as sjid,cs.fx_price,cs.commission,cs.expire_day,cs.add_time AS sj_time FROM {$this->db->dbprefix}course_shelves as cs left join {$this->db->dbprefix}course as v on(v.id = cs.course_id and cs.is_fenxiao = 1) WHERE $where and cs.school_id =".$this->user_data['school_id']." ORDER BY ".$order." LIMIT $limit, $offset";
            }
            

            $result = $this->db->query($sql)->result_array();
            if($data['search']){
                foreach ($result as $key => $value) {
                    $result[$key]['title'] = str_replace($data['search'],'<font color="red">'.$data['search'].'</font>',$value['title']);
                }
            }
            /*if($data['sj'] == 1){
                $sql = "SELECT count(v.id) AS count FROM {$this->db->dbprefix}course as v WHERE $where";
            }elseif($data['sj'] == 2){
                 $sql = "SELECT count(v.id) AS count FROM {$this->db->dbprefix}course as v WHERE $where $sj_id";
            }elseif($data['sj'] == 3){
               
                $sql = "SELECT count(cs.id) AS count FROM {$this->db->dbprefix}course_shelves as cs left join {$this->db->dbprefix}course as v on(v.id = cs.course_id and cs.is_fenxiao = 1) WHERE $where and cs.school_id =".$this->user_data['school_id']."";
            }*/
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
        }



        function get_fx_course_info($id = 0){
            $sql = "SELECT * from {$this->db->dbprefix}course where type = 1 and id = $id and school_id = 0";
            return $this->db->query($sql)->row_array();
        }



        function get_system_course_privilege(){
            $school_id = (int)$this->user_data['school_id'];
            $sql = "SELECT category_id from {$this->db->dbprefix}o2o_school_system_video_course where school_id = ".$school_id;
            $result = $this->db->query($sql)->result_array();
            return $result;
        }


        function get_code($id,$type){
             $school_id = (int)$this->user_data['school_id'];
            if($type == 1){
                $sql = "SELECT count(id) as count from {$this->db->dbprefix}o2o_school_system_video_course_codes where use_uid = 0 and school_id = $school_id and course_id=".$id;
            }
            if($type == 2){
                $sql = "SELECT count(id) as count from {$this->db->dbprefix}o2o_school_system_video_course_codes where use_uid > 0 and school_id = $school_id and course_id=".$id;
            }
            $result = $this->db->query($sql)->row_array();
            return $result['count'];
        }

    	
	}